<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
	>
		<template slot="query">
			<el-form
				ref="form"
				:inline="true"
				:model="queryData"
				class="flex-warp-container"
			>
				<el-form-item
					:label="`${$t('common.belong_merchant')}:`"
					class="tagheight flex-row-container"
					label-width="77px"
				>
					<merchant-select
						v-model="queryData.merchantId"
						:merchantInfo.sync="merchantObj"
					></merchant-select>
				</el-form-item>
				<el-form-item
					:label="`${$t('member.frozen_rebate_period')}:`"
				>
					<el-date-picker
							v-model="rebateAtTime"
							size="medium"
							:picker-options="pickerShortcut2"
							format="yyyy-MM-dd HH:mm:ss"
							type="datetimerange"
							range-separator="-"
							:start-placeholder="$t('common.start_date')"
							:end-placeholder="$t('common.end_date')"
							align="right"
							value-format="timestamp"
							style="width: 375px"
							:default-time="defaultTime"
							@focus="reloadTime"
						></el-date-picker>
				</el-form-item>
				<el-form-item
					:label="`${$t('funds.order_no')}:`"
					class="flex-row-container"
					label-width="61px"
				>
					<el-input
						v-model="queryData.orderNumber"
						clearable
						size="medium"
						:placeholder="$t('common.please_enter')"
						v-input="{ name: 'num_alp' }"
					></el-input>
				</el-form-item>
				<el-form-item :label="`${$t('member.frozen_rebate_amount')}:`">
					<el-input
						v-model="queryData.frozenAmountMin"
						v-input="{ name: 'float' }"
						size="medium"
						:placeholder="$t('common.minimum')"
						style="width: 133px"
						clearable
						name="frozenAmountMin"
						@blur="checkValue($event)"
					></el-input>
					-
					<el-input
						v-model="queryData.frozenAmountMax"
						v-input="{ name: 'float' }"
						size="medium"
						:placeholder="$t('common.maximum')"
						style="width: 133px"
						clearable
						name="frozenAmountMax"
						@blur="checkValue($event)"
					></el-input>
				</el-form-item>

				<el-form-item
					:label="`${$t('funds.user_name')}:`"
					class="flex-row-container"
					label-width="77px"
				>
					<el-input
						v-model="queryData.memberAccount"
						clearable
						size="medium"
						:placeholder="$t('common.please_enter')"
						:disabled="loading"
						v-input="{ name: 'num_alp' }"
						:maxlength="11"
						@keyup.enter.native="enterSearch"
					></el-input>
				</el-form-item>
				<el-form-item
					:label="
						`${$t('agent.agent_account')}:`
					"
					class="flex-row-container"
					label-width="77px"
				>
					<el-input
						v-model="queryData.agentAccount"
						clearable
						:maxlength="12"
						v-input="{ name: 'symbols' }"
						:placeholder="$t('common.please_enter')"
					></el-input>
				</el-form-item>
				<el-form-item style="margin-left: 25px">
					<el-button
						type="primary"
						icon="el-icon-search"
						:disabled="loading"
						size="medium"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						:disabled="loading"
						size="medium"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
					<el-button
						v-if="hasPermission('90870044')"
						icon="el-icon-download"
						type="warning"
						:disabled="loading"
						size="medium"
						:loading="exportBtnLoading"
						@click="handleExportExcel"
					>
						{{ $t('common.export') }}
					</el-button>
				</el-form-item>
			</el-form>
		</template>
		<template slot="content">
			<div v-loading="loading">
				<el-table
					border
					size="mini"
					:data="tableData"
					style="width: 100%"
					class="small-size-table"
					:max-height="tableHeight"
					:header-cell-style="getRowClass"
					element-loading-spinner="el-icon-loading"
				>
					<el-table-column
						prop="orderNumber"
						align="center"
						min-width="220"
						:label="$t('funds.order_no')"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							<Copy
								v-if="!!scope.row.orderNumber"
								:title="scope.row.orderNumber"
								:copy="copy"
							>
								{{ scope.row.orderNumber || '-' }}
							</Copy>
							<span v-else>-</span>
						</template>
					</el-table-column>
					<el-table-column
						prop="memberAccount"
						align="center"
						:label="$t('funds.user_name')"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							<Copy
								v-if="!!scope.row.memberAccount"
								:title="scope.row.memberAccount"
								:copy="copy"
							>
								{{ scope.row.memberAccount || '-' }}
							</Copy>
							<span v-else>-</span>
						</template>
					</el-table-column>
					<el-table-column
						prop="accountType"
						align="center"
						:label="`${$t('common.number_type')}`"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							{{ scope.row.accountType || '-' }}
						</template>
					</el-table-column>
					<el-table-column
						prop="belongingMerchant"
						align="center"
						:label="$t('common.belong_merchant')"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							{{ scope.row.belongingMerchant || '-' }}
						</template>
					</el-table-column>
					<el-table-column
						prop="directAgent"
						align="center"
						:label="$t('common.top_agent')"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							<Copy
								v-if="!!scope.row.directAgent"
								:title="scope.row.directAgent"
								:copy="copy"
							>
								{{ scope.row.directAgent || '-' }}
							</Copy>
							<span v-else>-</span>
						</template>
					</el-table-column>
					<el-table-column
						prop="belongingGeneralAgent"
						align="center"
						:label="$t('member.belonging_general_agent')"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							<Copy
								v-if="!!scope.row.belongingGeneralAgent"
								:title="scope.row.belongingGeneralAgent"
								:copy="copy"
							>
								{{ scope.row.belongingGeneralAgent || '-' }}
							</Copy>
							<span v-else>-</span>
						</template>
					</el-table-column>
					<el-table-column
						prop="personalFrozenRebate"
						align="center"
						min-width="180"
						:label="
							$t('member.personal_frozen_rebate')
						"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							<el-link
								type="primary"
								@click="dialogData(scope.row)"
							>
								{{ handleCurrency('$') }}
								{{ handleNumber('$', scope.row.personalFrozenRebate) }}
							</el-link>
						</template>
					</el-table-column>
					<el-table-column
						prop="frozenRebatePeriod"
						align="center"
						:label="$t('member.frozen_rebate_period')"
						show-overflow-tooltip
					>
						<template slot-scope="scope">
							{{ scope.row.frozenRebatePeriod || '-' }}
						</template>
					</el-table-column>
				</el-table>
				<!-- 分页 -->
				<el-pagination
					:current-page.sync="pageNum"
					class="pageValue"
					background
					layout="total, sizes,prev, pager, next, jumper"
					:page-size="pageSize"
					:page-sizes="pageSizes"
					:total="total"
					:pager-count="9"
					@current-change="handleCurrentChange"
					@size-change="handleSizeChange"
				></el-pagination>
			</div>
		</template>
		<detail-page
			v-if="showDialog"
			:rowVal="rowVal"
			:closeDialog="
				() => {
					showDialog = false
				}
			"
		/>
	</main-content>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { routerNames } from '@/utils/consts'
import summaryList from './components/summaryList'
import MainContent from '@/components/MainContent/index.vue'

const startTime = () => {
	return dayjs().subtract(1, 'day').startOf('day').valueOf()
}
const endTime = () => {
	return dayjs().subtract(1, 'day').endOf('day').valueOf()
}

export default {
	name: routerNames.memberFrozenRebateRecord,
	components: {
		DetailPage: () => import('./components/detail-page.vue'),
		MainContent
	},
	mixins: [list, summaryList],
	data() {
		return {
			tableHeight: 480,
			otherDependenComponents: [],
			queryData: {},
			rebateAtTime: [startTime(), endTime()],
			tableData: [],
			subSummary: {},
			totalSummary: {},
			rowVal: {},
			showDialog: false,
			merchantObj: '',
			exportBtnLoading: false,
			loading: false
		}
	},
	computed: {
		rebateType() {
			return this.globalDics.rebateType || []
		},
		appIdarr() {
			return this.globalDics.appId || []
		}
	},
	methods: {
		loadData() {
			this.loading = true
			const rebateAt = this.rebateAtTime || []
			if(this.queryData.orderNumber === '') this.queryData.orderNumber = undefined
			if(this.queryData.frozenAmountMin === '') this.queryData.frozenAmountMin = undefined
			if(this.queryData.frozenAmountMax === '') this.queryData.frozenAmountMax = undefined
			if(this.queryData.memberAccount === '') this.queryData.memberAccount = undefined
			if(this.queryData.agentAccount === '') this.queryData.agentAccount = undefined
			const params = {
				...this.queryData,
				frozenStartAt: rebateAt[0]
					? dayjs(rebateAt[0]).format('YYYY-MM-DD HH:mm:ss')
					: undefined,
				frozenEndAt: rebateAt[1]
					? dayjs(rebateAt[1]).format('YYYY-MM-DD HH:mm:ss')
					: undefined,
				...this.getParams()
			}
			this.pageCurrency = this.merchantObj?.currency || ''
			this.$api
				.memberFrozenRebateRecord(params)
				.then((res) => {
					const { record, summary, totalRecord } = res.data || {}
					this.tableData = record || []
					this.subSummary = summary || {}
					this.total = totalRecord || 0

					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
		},
		search() {
			this.pageNum = 1
			this.loadData()
		},
		reset() {
			this.queryData = {}
			this.merchantObj = ''
			this.rebateAtTime = [startTime(), endTime()]
			this.pageNum = 1
			this.loadData()
		},
		reloadTime() {
			this.rebateAtTime = [startTime(), endTime()]
		},
		dialogData(val) {
			this.rowVal = {
				memberId: val.memberAccountId,
				memberAccount: val.memberAccount,
				frozenRebatePeriod: val.frozenRebatePeriod
			}
			this.showDialog = true
		},
		handleExportExcel() {
			const createdAt = this.rebateAtTime || []
			if(this.queryData.orderNumber === '') this.queryData.orderNumber = undefined
			if(this.queryData.frozenAmountMin === '') this.queryData.frozenAmountMin = undefined
			if(this.queryData.frozenAmountMax === '') this.queryData.frozenAmountMax = undefined
			if(this.queryData.memberAccount === '') this.queryData.memberAccount = undefined
			if(this.queryData.agentAccount === '') this.queryData.agentAccount = undefined
			const params = {
				...this.queryData,
				frozenStartAt: createdAt[0]
					? dayjs(createdAt[0]).format('YYYY-MM-DD HH:mm:ss')
					: undefined,
				frozenEndAt: createdAt[1]
					? dayjs(createdAt[1]).format('YYYY-MM-DD HH:mm:ss')
					: undefined
			}
			this.exportBtnLoading = true
			this.exportExcelPublic({
				api: 'getMemberRebateRecordReportExport',
				params
			}).finally(() => {
				this.exportBtnLoading = false
			})
		},
		checkValue(e) {
			const { name, value } = e.target
			switch (name) {
				case 'frozenAmountMin':
					if (
						!!this.queryData.frozenAmountMax &&
						value &&
						value * 1 > this.queryData.frozenAmountMax * 1
					) {
						this.$message({
							type: 'warning',
							message: `${this.$t(
								'member.please_enter_less_than'
							) +
								this.queryData.frozenAmountMax +
								this.$t('member.amount_of_money')}`
						})
						this.queryData.frozenAmountMin = undefined
					} else {
						this.queryData.frozenAmountMin = value
					}
					break
				case 'frozenAmountMax':
					if (
						!!this.queryData.frozenAmountMin &&
						value &&
						value * 1 < this.queryData.frozenAmountMin * 1
					) {
						this.$message({
							type: 'warning',
							message: `${this.$t(
								'member.please_enter_greater_than'
							) +
								this.queryData.frozenAmountMin +
								this.$t('member.amount_of_money')}`
						})
						this.queryData.frozenAmountMax = undefined
					} else {
						this.queryData.frozenAmountMax = value
					}
					break
			}
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
	color: #909399;
	font-weight: 700;
}

::v-deep .el-table__fixed-footer-wrapper .cell,
::v-deep .el-table__footer-wrapper .cell {
	height: 100%;
	padding: 0 !important;
	margin: 0 !important;

	> div {
		height: 100%;
	}
}

::v-deep .footer_count_row {
	min-height: 50px;
	height: 50%;
	display: flex;
	font-weight: 700;
	padding: 5px 0;
	align-items: center;
	flex-direction: column;
	justify-content: center;
}

::v-deep .footer_count_row_border {
	border-top: 1px solid #ebeef5;
}
</style>
